#include <stdio.h>
#include "common_list.h"

struct ListNode* reverseList2(struct ListNode* head) {
    printf("\n方法2巧妙，通过将pre设置为NULL，cur设置为head，可以避免方法1中那样特殊处理第一个节点。");

    struct ListNode* pre = NULL;
    struct ListNode* cur = head;
    struct ListNode* tmp;

    int is_head = 1;
    while(cur) {
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }
    return pre;
}

struct ListNode* reverseList(struct ListNode* head) {
    if (!head)
        return head;
    struct ListNode* pre = head;
    struct ListNode* cur = head->next;
    struct ListNode* tmp;

    int is_head = 1;
    while(cur) {
        tmp = cur->next;
        if (is_head) {
            is_head = 0;
            pre->next = NULL;
        }
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }
    return pre;
}

int main(void)
{
    printf("#206. 反转链表\n");
    struct ListNode *head = generate_list(4);
    print_list(head);
    head = reverseList(head);
    print_list(head);
    head = reverseList2(head);
    print_list(head);
    return 0;
}